﻿using CoreWeb.Utility.Filter;
using Interface;
using Microsoft.AspNetCore.Mvc;

namespace CoreWeb.Controllers
{
    public class FifthController : Controller
    {
        /// <summary>
        /// filter
        /// 1 IResourceFilter-OnResourceExecuting发生在控制器实例化之前
        /// 1.5实例化控制器
        /// 2 IActionFilter-OnActionExecuting
        /// 2.5开始进入action
        /// 3 IActionFilter-OnActionExecuted
        /// 4 IResultFilter-OnResultExecuting
        /// 4.5开始加载view
        /// 5 IResultFilter-OnResultExecuted  //视图替换环节
        /// 6 IResourceFilter-OnResourceExecuted
        /// 不同环节都可以做缓存，只是效果不一样，最优解肯定是IResourceFilter，在进程没有进入控制器之前把缓存返回出去
        /// 
        /// CustomCacheResourceFilterAttribute 基于IResourceFilter 完成了缓存，避免了控制器实例化和Action执行，但是视图重新执行了
        /// 
        /// 多个步骤，某个步骤需要缓存结果，Filter不适合的
        /// 可以基于autofac的AOP缓存service的结果 ---这个没写
        /// 
        /// 怎么可以不执行视图，或者直接重用视图的结果，直接宠用HTML？
        /// ResponseCache：在请求响应式，添加了一个responseheader，来指导浏览器缓存结果
        /// CustomCacheResultFilterAttribute 相当于重写了一边ResponseCache
        /// 
        /// 中间件怎么缓存
        /// 结合ResponseCache，在中间件完成拦截，可以完全不进入MVC
        /// </summary>
        private readonly ILogger<FifthController> _logger; 
        private readonly ITestServiceA _itsA;
        private readonly ITestServiceB _itsB;
        private readonly ITestServiceC _itsC;
        private readonly ITestServiceD _itsD;
        private readonly ITestServiceE _itsE; 
        public FifthController(ILogger<FifthController> logger, ITestServiceA itsA, ITestServiceB itsB, ITestServiceC itsC, ITestServiceD itsD, ITestServiceE itsE)
        {
            _logger = logger;
            _itsA = itsA;
            _itsB = itsB;
            _itsC = itsC;
            _itsD = itsD;
            _itsE = itsE;
        }
        [CustomActionFilter]
        [CustomResultFilter]
        [CustomResourceFilter]
        public IActionResult Index()
        {
            _logger.LogInformation("123456734");
            return View();
        }
        // [CustomCacheResourceFilterAttribute]
        [ResponseCache(Duration =600)]
        //  [CustomCacheResultFilterAttribute(Duration =60)]
        public IActionResult Info()
        { 
            ViewBag.Now= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            return View();
        }
        [CustomActionFilter]
        public IActionResult Infomation()
        {
            Console.WriteLine("this is Infomation action");
            _itsA.Show();
            return View();
        }
    }
}
